package com.easymin.weixin.common.directive;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.util.Map;

@SuppressWarnings("all")
public class ExtendsDirective implements TemplateDirectiveModel {

  public final static String DIRECTIVE_NAME = "extends";

  @SuppressWarnings("rawtypes")
  @Override
  public void execute(Environment env, Map params, TemplateModel[] loopVars,
      TemplateDirectiveBody body) throws TemplateException, IOException {

    String name = DirectiveUtils.getRequiredParam(params, "name");
    String encoding = DirectiveUtils.getParam(params, "encoding", null);

    String dir = getTemplatePath(env);

    String includeTemplateName = env.toFullTemplateName(dir, name);
    env.include(includeTemplateName, encoding, true);
  }

  private String getTemplatePath(Environment env) {
    String templateName = env.getTemplate().getName();

    return templateName.lastIndexOf('/') == -1 ? "" : templateName.substring(0,
        templateName.lastIndexOf('/') + 1);
  }

}
